import Vue from 'vue'
import Vuex from 'vuex'
import request from "@/utils/request";
import router from "@/router";
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        currentPathName: '',
        data: [
            {
            value: '点数据',
            label: '点数据',
            children: []
            },
            {
             value: '轨迹线数据',
             label: '轨迹线数据',
             children: []
            },
            {
            value: 'OD线数据',
            label: 'OD线数据',
            children: []
            },
            {
                value: 'shp数据',
                label: 'shp数据',
                children: []
            },
        ],
        dataPoint: [
            {
                value: '点数据',
                label: '点数据',
                children: []
            }
        ],
        dataLineTrace: [
            {
                value: '轨迹线数据',
                label: '轨迹线数据',
                children: []
            },
        ],
        dataLineOD: [
            {
                value: 'OD线数据',
                label: 'OD线数据',
                children: []
            },
        ],
    },
    mutations: {
        setPath (state) {
            state.currentPathName = localStorage.getItem("currentPathName")
        },
        logout() {
            // 清空缓存
            localStorage.removeItem("user")
            localStorage.removeItem("menus")
            router.push("/login")
        },
        loadMenuData(state){
            for (let i = 0; i <state.data.length ; i++) {
                state.data[i].children=[]
            }
            request.get("/dataMenu").then(res => {
                for (let i = 0; i < res.data.length; i++) {
                    if (res.data[i].father === state.data[0].label){
                        state.data[0].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                    }else if (res.data[i].father === state.data[1].label){
                        state.data[1].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                    }else if (res.data[i].father === state.data[2].label){
                        state.data[2].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                    }else if (res.data[i].father === state.data[3].label){
                        state.data[3].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                    }
                }
            })
        },
        loadMenuDataPoint(state){
            for (let i = 0; i <state.data.length ; i++) {
                state.data[i].children=[]
            }
            request.get("/dataMenu").then(res => {
                for (let i = 0; i < res.data.length; i++) {
                    if (res.data[i].father === state.dataPoint[0].label){
                        state.dataPoint[0].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                    }
                }
            })
        },
        loadMenuDataLineTrace(state){

            for (let i = 0; i <state.data.length ; i++) {
                state.data[i].children=[]
            }
            request.get("/dataMenu").then(res => {
                // state.dataLineTrace[0].children = []
                for (let i = 0; i < res.data.length; i++) {
                    if (res.data[i].father === state.dataLineTrace[0].label){
                        // console.log(res.data[i].father,'res.data[i].father');
                        state.dataLineTrace[0].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                        console.log(state.dataLineTrace[0].children);
                    }
                }
            })
        },
        loadMenuDataLineOD(state){

            for (let i = 0; i <state.data.length ; i++) {
                state.data[i].children=[]
            }
            request.get("/dataMenu").then(res => {
                // state.dataLineTrace[0].children = []
                for (let i = 0; i < res.data.length; i++) {
                    if (res.data[i].father === state.dataLineOD[0].label){
                        // console.log(res.data[i].father,'res.data[i].father');
                        state.dataLineOD[0].children.push(
                            {value: res.data[i].name,label:res.data[i].name}
                        )
                        console.log(state.dataLineTrace[0].children);
                    }
                }
            })
        },
        refresh(state){
            state.dataLineTrace[0].children = [];
            state.dataPoint[0].children = [];
            state.dataLineOD[0].children = [];
        },
    }
})

